import { Component, Injector, ElementRef, ViewChildren, QueryList } from '@angular/core';
import { IonicPage, NavController, NavParams, ActionSheetController, Content } from 'ionic-angular';
import { BasePage } from '../../../basePage';
import { StorageService } from './../../../../providers/StorageService';
import { nailerService } from './../../../../providers/nailerService';
import { PIC_URL, QUALITY_SIZE, CODE_PUSH_DEPLOYMENT_KEY } from "./../../../../providers/Config";
declare var $: any
/**
 * Generated class for the ShopServicePage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-shop-service',
  templateUrl: 'shop-service.html',
})
export class ShopServicePage extends BasePage {
  issueObj: any;
  @ViewChildren("shopContainer") content: Content;

  cateItemDiv: ElementRef;

  _elementRef: any;




  picrooturl = '';
  servicelist = [];
  userVo = {};//获取后台返回回来的数据
  userApi = { //需要修改的值
    "add_type": 0,
    "user_id": 0,    /**用户id */
    "service_id": 0, /**服务id */
    "nail_id": 0,   /**美甲id */
    "artist_id": 0,  /**美甲师id */
    "service_price": '0.00',
    "market_price": '0.00',
    "service_name": '',
    "tools": '',
    "material": '',
    "imgs": '/Uploads/nail_album/2017-05-24/592515487bcfe_thumb_328-340.jpg',
    "is_buy": 0,
    "consuming_time": 0
  };
  userinfo: {};

  imgarr = []/**存储图片数组 */

  oper: 1;

  constructor(injector: Injector, private elementRef: ElementRef, public nailerservice: nailerService, public actionSheetCtrl: ActionSheetController, public stc: StorageService) {
    super(injector);
    this.picrooturl = PIC_URL
    this.userinfo = this.stc.read('userinfo');
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad ShopServicePage');
    this.getShopBuyNailListAPI();
    this.userApi.user_id = this.userinfo['user_id'];
    this.oper = this.navParams.get('oper');
    /**2修改服务 1创建服务 */
    if (this.oper == 1) {
      this.getownerself();
    } else if (this.oper == 2) {
      this.getParams();
    } else {
      this.editparams();
    }
  }
  dismissCLI() {
    this.navCtrl.pop()
  }
  /**继续发布服务 */
  gotopublic() {
    /**2修改 1新增 */
    let index = this.servicelist['index'];
    if (typeof index == 'undefined' || index == -1) {
      let toast = this.toastCtrl.create({
        message: '没有要发布的服务，请选择要发布的服务或者发布其他服务',
        duration: 3000,
        position: 'top',
      })
      toast.present();
      return false;
    }
    this.navCtrl.push('IssueImgPage', { type: 2,detailObj:this.issueObj });
  }
  

  /**发布其他服务 */
  gotoother() {
    this.navCtrl.push('IssueImgPage', { type: 1 });
  }



  /**选择图片 */
  selectimg(index,item) {
    console.log(this.servicelist);
    this.userApi.service_name = this.servicelist[index].nail_name;
    this.userApi.service_price = this.servicelist[index].order_price;
    this.userApi.market_price = "0";
    this.userApi.tools = this.servicelist[index].nail_tools;
    this.userApi.material = this.servicelist[index].nail_materials;
    this.userApi.consuming_time = this.servicelist[index].nail_consuming_time;
    for (let i = 0; i < this.servicelist.length; i++) {
      if (i == index) {
        //$(".happy").eq(index).siblings().find("div").slideUp();
        this.servicelist[i]['selectimg'] = "assets/img/service.png";
        this.servicelist['index'] = index;
        console.log($(".happy").eq(index));
        $(".happy").eq(index).find("#grow").slideToggle("fast");
        this.issueObj=item;
      } else {
        this.servicelist[i]['selectimg'] = "assets/img/serviceno.png";
      }
    }
  }

  /**获取发布列表 */
  async getShopBuyNailListAPI() {
    let param = { 'shop_id': this.userinfo['shop_id'] };
    let result = await this.nailerservice.getShopBuyNailListAPI(param);
    console.log(result)
    if (result.code != 1) {
      let toast = this.toastCtrl.create({
        message: result.msg,
        duration: 3000,
        position: 'top',
      })
      toast.present();
    } else {
      this.servicelist = result.data;
      console.log(this.servicelist);
      for (let i = 0; i < this.servicelist.length; i++) {
        this.servicelist[i]['selectimg'] = "assets/img/serviceno.png";
      }
      this.servicelist['index'] = -1;
    }
  }


  /*发布服务*/
  /**获取美甲师购买商品的服务信息 */
  getParams() {
    this.userApi.add_type = 0;
    this.userApi.service_name = this.navParams.get('nail_name');
    this.userApi.nail_id = this.navParams.get('nail_id');
    this.userApi.artist_id = this.navParams.get('seller_id');
    this.userApi.tools = this.navParams.get('tools');
    this.userApi.material = this.navParams.get('nail_materials');
    this.userApi.tools = this.navParams.get('nail_tools');
    this.userApi.service_price = this.navParams.get('order_price');
    this.userApi.is_buy = 1;
    let imgstr = this.navParams.get('nail_album');
    if (imgstr != undefined) {
      this.splitimgstr(imgstr);
    }
    delete this.userApi.service_id;
  }

  /**发布自己的服务信息 */
  getownerself() {
    this.userApi.add_type = 0;
    this.userApi.nail_id = 0;
    this.userApi.artist_id = 0;
    this.userApi.is_buy = 0;
  }


  /**编辑服务 */
  editparams() {
    this.userApi.service_id = this.navParams.get('service_id');
    this.userApi.consuming_time = this.navParams.get('consuming_time');
    this.userApi.service_name = this.navParams.get('service_name');
    this.userApi.service_price = this.navParams.get('service_price');
    this.userApi.tools = this.navParams.get('tools');
    this.userApi.material = this.navParams.get('material');
    this.userApi.market_price = this.navParams.get('market_price');
    this.userApi.add_type = 1;
    this.userApi.is_buy = this.navParams.get('is_buy');
    this.userApi.artist_id = this.navParams.get('artist_id');
    let imgstr = this.navParams.get('imgs');
    if (imgstr != undefined) {
      this.splitimgstr(imgstr);
    }
    delete this.userApi.nail_id;
  }



  /**分割图片字符串为图片数组 */
  splitimgstr(imgstr) {
    if (imgstr.indexOf(",") != -1) {
      this.imgarr = imgstr.split(",");
    } else {
      this.imgarr[0] = imgstr;
    }
  }





  /**发布服务 */
  publicok() {
    this.addShopServerAPI();
  }


  /**从美甲师购买中发布美甲服务 */
  async addShopServerAPI() {
    console.log(this.userApi);
    let result = await this.nailerservice.addShopServerAPI(this.userApi);
    if (result.code != 1) {
      let toast = this.toastCtrl.create({
        message: result.msg,
        duration: 3000,
        position: 'top',
      })
      toast.present();
      return false;
    } else {
      this.showAlert('发布成功');
      this.navCtrl.push('StoreShowPage');
    }
  }


  presentActionSheetCamr() {
    let actionSheet = this.actionSheetCtrl.create({
      title: '选择',
      buttons: [
        {
          text: '拍照上传',
          handler: () => {

          }
        }, {
          text: '从相册上传',
          handler: () => {

          }
        },

        {
          text: '取消',
          role: 'cancel',
          handler: () => {
            console.log('Cancel clicked');
          }
        }
      ]
    });
    actionSheet.present();
  }
}
